package com.doge.walknovel.business.home.profile.topup

import androidx.lifecycle.MutableLiveData
import com.doge.walknovel.api.*
import com.doge.walknovel.api.model.RechargeProductInfo
import com.doge.walknovel.api.model.TopUpInfo
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.utils.*

class TopUpRepository {
    val loading = MutableLiveData<LoadStatus>()
    val dataList = MutableLiveData<List<RechargeProductInfo>>()
    val tips = MutableLiveData<String?>()

    fun request() {
        require(isMainThread())
        api
            .rechargeRuleListV2()
            .map(ApiResultMapTransformer())
            .subscribeOnIO()
            .observeOnMain()
            .subscribe(object : ObserverAdapter<TopUpInfo>() {
                override fun onNext(t: TopUpInfo) {
                    dataList.value = t.ruleList ?: listOf()
                    tips.value = t.tips
                    loading.value = LoadStatus.SUCCESS
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    loading.value = LoadStatus.FAILURE
                }
            })
    }

}